package com.zx.wangyixinwen.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.TopicActivity;
import com.zx.wangyixinwen.adapter.JokesAdapter;
import com.zx.wangyixinwen.adapter.TopicAdapter;
import com.zx.wangyixinwen.bean.Jokes;
import com.zx.wangyixinwen.bean.JokesBeen;
import com.zx.wangyixinwen.bean.Topic;
import com.zx.wangyixinwen.bean.TopicBean;
import com.zx.wangyixinwen.refresh.SwipeRefreshLoadingLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/6.
 */
public class JokesFragment extends Fragment  {
    private GridView gridView;
    private RequestQueue queue;
    private List<Jokes> datas;
    private JokesAdapter adapter;
    private Context context;
    private String url = "http://c.3g.163.com/recommend/getChanListNews?channel=" +
            "duanzi&size=20&passport=&devId=PW1Khnct4KlvXor6LqVfyw%3D%3D&lat=f0DW" +
            "2XFDMLut0ZRbS8JKtQ%3D%3D&lon=GxJugx%2BXUXN9%2Bl3twu9c6w%3D%3D&version" +
            "=5.4.6&net=wifi&ts=1453432442&sign=OGYPn6UdCT8Px3bSCawI%2FCWl%2BH5wx7zw4ZuonQ" +
            "0KCFV48ErR02zJ6%2FKXOnxX046I&encryption=1&canal=QQ_news";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_entertainment, null);
        gridView = (GridView) view.findViewById(R.id.gridView_jokes);
        addData();
        return view;
    }

    private void addData() {
        queue = Volley.newRequestQueue(getActivity());
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                datas = new ArrayList<>();
                final Jokes jokes = gson.fromJson(response, Jokes.class);
                datas.add(jokes);
                for (int i = 0; i < jokes.get段子().size(); i++) {
                    JokesBeen jokesBeen = new JokesBeen();
                    jokesBeen.setDigest(jokes.get段子().get(i).getDigest());
                    jokesBeen.setUpTimes(jokes.get段子().get(i).getUpTimes());
                }

                adapter = new JokesAdapter(context, datas);
                gridView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

}
